#!/bin/bash

set -e
set -o pipefail
set -x

targets=($(find bsp -name settings.mk | cut -d '/' -f 2))

programs=($(ls software))

SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"

CONFIGURATION=debug

# Run regression tests
for target in ${targets[@]}
do
    for program in "${programs[@]}"
    do
        # Coremark and Dhrystone don't fit
        if [ "${program}" == "dhrystone" -o \
             "${program}" == "coremark" -o  \
             "${program}" == "example-buserror" ] ; then
            continue
        fi

        # FreeRTOS examples are broken with Segger Libc
        if [ "${RISCV_LIBC}" == "segger" ] ; then
	    if [ "${program}" == "example-freertos-blinky" -o \
                 "${program}" == "example-freertos-pmp-blinky" -o \
                 "${program}" == "example-freertos-minimal" ] ; then
                continue
            fi
        fi

	# mc examples are broken
	if [ "${program}" == "example-freertos-mc" -o "${program}" == "example-freertos-blinky-mc" ]; then
	    continue
	fi

        # example-rnmi is only supported on qemu-sifive-e31-customer bsp
        if [ "${program}" == "example-rnmi" -a "${target}" != "qemu-sifive-e31-customer" ]; then
            continue
        fi

        make TARGET="${target}" PROGRAM="${program}" CONFIGURATION="${CONFIGURATION}" -j${nproc} software
        make TARGET="${target}" PROGRAM="${program}" CONFIGURATION="${CONFIGURATION}" clean-software
    done
    make TARGET="${target}" clean
done

echo "PASSED"

